/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.processing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import dk.redia.lib.log.Log;

public class AsyncTaskManager {

	public static final int MAX_CONCURRENT_TASK_COUNT = 3;
	public static AsyncTaskManager instance;
	
	private static final String logTag = "AsyncTaskManager";
	
	private List<ManagedAsyncTask<?, ?, ?>> readyTasks = Collections.synchronizedList(new ArrayList<ManagedAsyncTask<?,?,?>>());
	private Set<ManagedAsyncTask<?, ?, ?>> runningTasks = Collections.synchronizedSet(new HashSet<ManagedAsyncTask<?,?,?>>());
	
	public void taskCancelled(ManagedAsyncTask<?, ?, ?> task) {
		Log.d(logTag, "Task cancelled: " + task.getClass());
		removeTask(task);
	}

	public void taskFinished(ManagedAsyncTask<?, ?, ?> task) {
		Log.d(logTag, "Task finished: " + task.getClass());
		removeTask(task);
	}
	
	private void removeTask(ManagedAsyncTask<?, ?, ?> task) {
		boolean taskRemoved = false;
		if (taskRemoved = readyTasks.remove(task)) {
			Log.d(logTag, "Removed task from list of ready tasks");
		}
		if (taskRemoved = runningTasks.remove(task)) {
			Log.d(logTag, "Removed task from list of running tasks");
		}
		// If there is room, fire up another task
		if (taskRemoved) {
			checkAndRunNextTask();
		}
	}
	
	private void checkAndRunNextTask() {
		if (runningTasks.size()<MAX_CONCURRENT_TASK_COUNT && readyTasks.size()>0) {
			ManagedAsyncTask<?, ?, ?> nextPendingTask = readyTasks.remove(0);
			runningTasks.add(nextPendingTask);
			Log.d(logTag, "Executing task " +  nextPendingTask.getClass());
			// This in turn calls the parameterized version of execute
			// inside the task
			nextPendingTask.execute();
		}
		Log.d(logTag, "Current running task count: " + runningTasks.size());
		Log.d(logTag, "Current ready task count:   " + readyTasks.size());
	}
	
	public void taskReady(ManagedAsyncTask<?, ?, ?> task) {
		Log.d(logTag, "Task ready: " + task.getClass());
		readyTasks.add(task);
		checkAndRunNextTask();
	}
	
	private AsyncTaskManager() {
		
	}
	
	public static AsyncTaskManager getInstance() {
		if (instance==null) {
			instance = new AsyncTaskManager();
		}
		return instance;
	}

}
